<?php

use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this \yii\web\View
 * @var $user \app\models\User
 */
$user = Yii::$app->user->identity;
?>

<div id="sidebar" class="sidebar responsive sidebar-fixed sidebar-scroll">
    <!--
    <div class="sidebar-shortcuts" id="sidebar-shortcuts">
        <div class="sidebar-shortcuts-large" id="sidebar-shortcuts-large">
            <?php echo Html::a('<i class="ace-icon fa fa-plus"></i>', ['/'], ['class' => 'btn btn-success']); ?>
            <?php echo Html::a('<i class="ace-icon fa fa-book"></i>', ['/'], ['class' => 'btn btn-info']); ?>
            <?php echo Html::a('<i class="ace-icon fa fa-users"></i>', ['/user/user/index'], ['class' => 'btn btn-warning']); ?>
            <?php echo Html::a('<i class="ace-icon fa fa-signal"></i>', ['/'], ['class' => 'btn btn-danger']); ?>
        </div>
        <div class="sidebar-shortcuts-mini" id="sidebar-shortcuts-mini">
            <?php echo Html::a('', ['/'], ['class' => 'btn btn-success']); ?>
            <?php echo Html::a('', ['/'], ['class' => 'btn btn-info']); ?>
            <?php echo Html::a('', ['/user/user/index'], ['class' => 'btn btn-warning']); ?>
            <?php echo Html::a('', ['/'], ['class' => 'btn btn-danger']); ?>
        </div>
    </div>
    -->

    <?php
    /**
     * 检查菜单是否需要设置为当前状态
     * @param string $route
     * @return boolean
     */
    function checkMenuActive($route)
    {
        $controller = Yii::$app->controller;
        if (!$controller) {
            return false;
        }
        if ($controller->module->id != 'user') {
            return false;
        }
        $route = preg_split('/\//', $route);
        if (count($route) == 1) {
            if ($controller->id == $route[0]) {
                return true;
            }
        } elseif (count($route) == 2) {
            if ($controller->id == $route[0] && $controller->action->id == $route[1]) {
                return true;
            }
        }
        return false;
    }
    if (YII_ENV === 'prod') {
        $items = null; // Yii::$app->cache->get('user_sidebar_items_' . $user->id);
    } else {
        $items = null;
    }
    if (empty($items)) {
        $items = [
            ['label' => '物联网卡管理', 'icon' => 'fa fa-wifi', 'active' => 'card', 'items' => [
                ['label' => '卡列表', 'url' => ['/user/card/list'], 'active' => 'card/list'],
                ['label' => '到期提醒卡列表', 'url' => ['/user/card/list-end-day-alert', 'search_end_day_alert' => 60], 'active' => 'card/list-end-day-alert'],
                ['label' => '超流量卡列表', 'url' => ['/user/card/list-overload', 'search_overload' => 1], 'active' => 'card/list-overload'],
                ['label' => '卡属性', 'url' => ['/user/card/attr'], 'active' => 'card/attr'],
                ['label' => '卡替换', 'url' => ['/user/card/replace-list'], 'active' => 'card/replace-list'],
                ['label' => '批量设置属性', 'url' => ['/user/card/batch-set-attr'], 'active' => 'card/batch-set-attr'],
                ['label' => '账单列表', 'url' => ['/user/card/user-bill-list'], 'active' => 'card/user-bill-list'],
            ]],
            ['label' => '子账号管理', 'icon' => 'fa fa-users', 'active' => 'user', 'items' => [
                ['label' => '子账号列表', 'url' => ['/user/user/list'], 'active' => 'user/list'],
            ]],
        ];
        Yii::$app->cache->set('user_sidebar_items_' . $user->id, $items, 86400);
    }?>

    <ul class="nav nav-list">
        <li class="">
            <a href="<?php echo Url::to(['/user']); ?>">
                <i class="menu-icon fa fa-tachometer"></i>
                <span class="menu-text"> 控制台</span>
            </a>

            <b class="arrow"></b>
        </li>
        <?php foreach ($items as $item) {
            if (empty($item)) {
                continue;
            } ?>
            <li class="<?php if (checkMenuActive($item['active'])) {
                echo 'open active';
            } ?>">
                <a href="#" class="dropdown-toggle">
                    <i class="menu-icon <?php echo $item['icon']; ?>"></i>
                    <span class="menu-text"><?php echo $item['label']; ?></span>
                    <b class="arrow fa fa-angle-down"></b>
                </a>
                <b class="arrow"></b>
                <ul class="submenu">
                    <?php if (isset($item['items']) && is_array($item['items'])) {
                        foreach ($item['items'] as $subitem) {
                            if (empty($subitem)) {
                                continue;
                            } ?>
                            <li class="<?php if (checkMenuActive($subitem['active'])) {
                                echo 'active';
                            } ?>">
                                <a href="<?php echo Url::to($subitem['url']); ?>">
                                    <i class="menu-icon fa fa-caret-right"></i>
                                    <?php echo $subitem['label']; ?>
                                </a>
                                <b class="arrow"></b>
                            </li>
                        <?php }
                    } ?>
                </ul>
            </li>
        <?php } ?>

    </ul>
    <div class="sidebar-toggle sidebar-collapse" id="sidebar-collapse">
        <i class="ace-icon fa fa-angle-double-left" data-icon1="ace-icon fa fa-angle-double-left"
           data-icon2="ace-icon fa fa-angle-double-right"></i>
    </div>
    <script>
        try {
            ace.settings.check('sidebar', 'collapsed');
        } catch (e) {
        }
    </script>
</div>

